<template>
  <div class="body">
    <div class="cart">
      <div class="cart-header">
        <div class="cart-header-title">用户信息</div>
        <div class="cart-header-main">
          <div class="cart-info">用户名称</div>
          <div class="cart-price">用户工号</div>
          <div class="cart-price">用户职位</div>
          <div class="cart-price">编辑</div>
        </div>
      </div>
      <div class="cart-content">
        <div class="cart-content-main" v-for="(item,index) in userList" :key="item.product_name">
          <div class="cart-info">
            <span>{{item.user_name}}</span>
          </div>
          <div class="cart-price">{{item.user_number}}</div>
          <div class="cart-price">{{item.user_position}}</div>
          <div class="cart-count">
            <el-button type="info" round @click="edit(index)">编辑</el-button>
          </div>
        </div>
      </div>
      <div class="cart-footer">
        <div class="cart-footer-desc">
          <div class="cart-control-order" @click="add">添加新用户</div>
        </div>
      </div>
      <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="用户名称" :label-width="formLabelWidth">
            <el-input v-model="form.user_name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="用户工号" :label-width="formLabelWidth" >
            <el-input v-model="form.user_number" autocomplete="off" :disabled="status==='edit'"></el-input>
          </el-form-item>
          <el-form-item label="用户初始密码" :label-width="formLabelWidth">
            <el-input v-model="form.user_password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="用户职称" :label-width="formLabelWidth">
            <el-select v-model="form.user_position" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="sure">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      status: '',
      leg: 0,
      imageUrl: "",
      userList: [],
      options: [
        {
          label: "店长",
          value: "店长"
        },
        {
          label: "收银员",
          value: "收银员"
        }
      ],
      form: {
        user_name: "",
        user_number: "",
        user_password: "",
        user_position: ""
      },
      formLabelWidth: '120px',
      dialogFormVisible: false
    };
  },
  computed: {
    productList() {
      return this.$store.state.productList;
    }
  },
  created() {
    this.getUserAll()
  },
  methods: {
    add(){
       this.status = 'add'
       this.form= {
        user_name: "",
        user_number: "",
        user_password: "",
        user_position: ""
      }
       this.dialogFormVisible = true
    },
    getUserAll(){
      this.$axios.get("/api/user/getUserAll").then(res => {
      console.log(res);
      this.userList = res.data;
    });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    edit(index){
       this.dialogFormVisible = true
       this.status = 'edit'
       this.form = this.userList[index]
       delete this.form.user_id
    },
    editUser() {
       this.form["user_id"] =
        parseInt(this.userList[this.userList.length - 1].user_id) + 1;
      this.$axios
        .get("/api/user/updateUser", { params: this.form })
        .then(res => {
          console.log(res);
          this.$message({
            message: "修改成功！",
            type: "success"
          });
          this.dialogFormVisible = false;
          this.getUserAll()
        })
        .catch(err => {
          this.$message({
            message: "修改失败！",
            type: "fail"
          });
          console.log(err);
        });
    },
    sure(){
       if(this.status === 'add'){
         this.addUser()
       }else if(this.status === 'edit'){
         this.editUser()
       }
    },
    addUser() {
      this.form["user_id"] =
        parseInt(this.userList[this.userList.length - 1].user_id) + 1;
      this.$axios
        .get("/api/user/save", { params: this.form })
        .then(res => {
          console.log(res);
          this.$message({
            message: "添加成功！",
            type: "success"
          });
          this.dialogFormVisible = false;
          this.getUserAll()
        })
        .catch(err => {
          this.$message({
            message: "添加失败！",
            type: "fail"
          });
          console.log(err);
        });
    },
  }
};
</script>
<style scoped>
.body {
  box-sizing: border-box;
  width: 100%;
  min-height: 100%;
  overflow: hidden;
  background: #f7f8f8;
}

.cart {
  box-sizing: border-box;
  margin: 32px;
  background: #fff;
  border: 1px solid #dddee1;
  border-radius: 10px;
}
.cart-header-title {
  padding: 16px 32px;
  border-bottom: 1px solid #dddee1;
  border-radius: 10px 10px 0 0;
  background: #f8f8f9;
}
.cart-header-main {
  text-align: center;
  padding: 8px 32px;
  overflow: hidden;
  border-bottom: 1px solid #dddee1;
  background-color: #eee;
  overflow: hidden;
}
.cart-empty {
  text-align: center;
  padding: 32px;
}
.cart-header-main div {
  box-sizing: border-box;
  text-align: center;
  float: left;
  padding: 25px;
}
div .cart-info {
  width: 70%;
  text-align: left;
}
.cart-count {
  width: 10%;
}
.cart-cost {
  width: 10%;
}
.cart-delete {
  width: 10%;
}
.cart-price {
  width: 10%;
}

.cart-desc {
  width: 20%;
}
.cart-content-main {
  padding: 0 32px;
  height: 60px;
  line-height: 60px;
  text-align: center;
  border-bottom: 1px dashed #e9eaec;
  overflow: hidden;
}
.cart-content-main div {
  float: left;
}
.cart-content-main img {
  width: 40px;
  height: 40px;
  margin-right: 10px;
  position: relative;
  top: 10px;
}
.cart-content-main div {
  float: left;
}
.cart-control-minus,
.cart-control-add {
  display: inline-block;
  margin: 0 4px;
  width: 24px;
  height: 24px;
  line-height: 22px;
  text-align: center;
  background-color: #f8f8f9;
  border-radius: 50%;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
  cursor: pointer;
}
.cart-control-delete {
  cursor: pointer;
  color: #2d8cf0;
}
.cart-control-order {
  display: inline-block;
  margin-right: 15px;
  padding: 15px 32px;
  border-radius: 6px;
  background: #2d8cf0;
  color: #fff;
  cursor: pointer;
}
.cart-footer {
  padding: 32px;
  text-align: center;
}
.cart-footer-desc {
  display: inline-block;
  padding: 0 16px;
}
.cart-footer-desc span {
  color: #f2352e;
  font-size: 20px;
}
.fixDiag {
  position: fixed;
  top: 100px;
  right: 60px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
